package com.adlx.dingdong.mall.core;

import com.adlx.dingdong.mall.client.ProductClient;
import com.adlx.dingdong.mall.domain.ShopGoods;
import com.adlx.dingdong.mall.domain.ShopGoodsCriteria;
import com.adlx.dingdong.mall.dto.ShopGoodsDTO;
import com.adlx.dingdong.mall.mapper.ShopGoodsMapper;
import com.adlx.dingdong.mall.vo.ShopGoodsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.adlx.dingdong.mall.commons.utils.Beans.batchTransform;

/**
 * Created by dulei on 18/1/9.
 */
@RefreshScope
@RestController
public class ProductClientImpl implements ProductClient {
    @Autowired
    private ShopGoodsMapper shopGoodsMapper;

    @RequestMapping(value = "/productList", method = RequestMethod.POST)
    public PageInfo getList(@RequestBody ShopGoodsDTO shopGoodsDTO) {
        if (null == shopGoodsDTO) {
            shopGoodsDTO = new ShopGoodsDTO();
        }
        PageHelper.startPage(shopGoodsDTO.getPageNum(), shopGoodsDTO.getPageSize());
        ShopGoodsCriteria criteria = new ShopGoodsCriteria();
        ShopGoodsCriteria.Criteria goodsCriteria = criteria.createCriteria();
        goodsCriteria.andIsputawayEqualTo(new Byte("1"));
        goodsCriteria.andIsenabledEqualTo(new Byte("1"));
        goodsCriteria.andIsdeletedEqualTo(new Byte("0"));
        if (null != shopGoodsDTO.getCategoryId()) {
            goodsCriteria.andCategoryidEqualTo(shopGoodsDTO.getCategoryId());
        }
        if (null != shopGoodsDTO.getType() && "hot".equals(shopGoodsDTO.getType())) {
            goodsCriteria.andIshotEqualTo(new Byte("1"));
        }
        if (null != shopGoodsDTO.getType() && "new".equals(shopGoodsDTO.getType())) {
            goodsCriteria.andIsnewEqualTo(new Byte("1"));
        }
        if (null != shopGoodsDTO.getType() && "vip".equals(shopGoodsDTO.getType())) {
            goodsCriteria.andIsspecialgoodsEqualTo(new Byte("1"));
        }

        criteria.setOrderByClause("Collating desc");
        List<ShopGoods> roleVoList = shopGoodsMapper.selectByExample(criteria);
        return new PageInfo(batchTransform(ShopGoodsVO.class, roleVoList));
    }


}
